package com.typhoon.spring_boot_demo1.controller;

import com.typhoon.spring_boot_demo1.manager.UserOptManager;
import com.typhoon.spring_boot_demo1.request.UserOptContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.typhoon.spring_boot_demo1.bean.User;
import com.typhoon.spring_boot_demo1.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class IndexController {

	@Autowired
	UserService userService;

	@Autowired
	UserOptManager userOptManager;
	
	@RequestMapping("/hello")
	public String index() {
		return "hello aeolus";
	}
	
	@RequestMapping("/get/{id}")
	public User queryUser(@PathVariable("id") Long id) {
		return this.userService.queryByPK(id);
	}


	@RequestMapping("/user/insert")
	public Map<String,Object> saveUser(HttpServletRequest request,
									   HttpServletResponse response,
									   @RequestParam("name") String name,
									   @RequestParam("sex") Integer sex,
									   @RequestParam("age") Integer age) {
		Map<String,Object> result = new HashMap<>();
		if(idParamIllegal(name,age,sex)) {
			result.put("success",false);
			result.put("msg","参数非法");
			return result;
		}
		try {
			UserOptContext  context = new UserOptContext();
			context.setName(name);
			context.setAge(age);
			context.setSex(sex);
			this.userOptManager.insert(context);
			result.put("success",true);
			result.put("msg","新增成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success",false);
			result.put("msg","新增操作异常");
		}
		return result;
	}

	@RequestMapping("/user/delete")
	public Map<String,Object> deleteUser(HttpServletRequest request,
									   HttpServletResponse response,
									   @RequestParam("id") Long id) {
		Map<String,Object> result = new HashMap<>();
		if(idParamIllegal(id)) {
			result.put("success",false);
			result.put("msg","参数非法");
			return result;
		}
		try {
			UserOptContext  context = new UserOptContext();
			context.setId(id);
			this.userOptManager.delete(context);
			result.put("success",true);
			result.put("msg","删除成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success",false);
			result.put("msg","删除操作异常");
		}
		return result;
	}

	@RequestMapping("/user/update")
	public Map<String,Object> updateUser(HttpServletRequest request,
										 HttpServletResponse response,
										 @RequestParam("id") Long id,
										 @RequestParam("name") String name,
										 @RequestParam("sex") Integer sex,
										 @RequestParam("age") Integer age
										 ) {
		Map<String,Object> result = new HashMap<>();
		if(idParamIllegal(id,name,age,sex)) {
			result.put("success",false);
			result.put("msg","参数非法");
			return result;
		}
		try {
			UserOptContext  context = new UserOptContext();
			context.setId(id);
			context.setName(name);
			context.setAge(age);
			context.setSex(sex);
			this.userOptManager.update(context);
			result.put("success",true);
			result.put("msg","更新成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success",false);
			result.put("msg","更新操作异常");
		}
		return result;
	}

	/**
	 * 检查参数是否非法
	 * @param params
	 * @return
	 */
	private boolean idParamIllegal(Object ... params) {
		for(Object obj : params) {
			if(null == obj) {
				return true;
			}
		}
		return false;
	}

}
